package com.pucheng.bracelet.ui.activity.steps

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ext.KEY_DATE
import com.pucheng.bracelet.app.ext.REQUEST_CODE_TO_STEP_HISTORY
import com.pucheng.bracelet.app.ext.REQUEST_CODE_TO_STEP_TAG
import com.pucheng.bracelet.data.bean.BarChartBean
import com.pucheng.bracelet.data.bean.StepsBean
import com.pucheng.bracelet.databinding.ActivityStepsBinding
import com.pucheng.bracelet.ui.fragment.steps.StepsToDayFragment
import com.pucheng.bracelet.ui.fragment.steps.StepsToMonthFragment
import com.pucheng.bracelet.ui.fragment.steps.StepsToWeekFragment
import com.pucheng.bracelet.ui.view.CircleProgressView
import com.pucheng.bracelet.viewmodel.steps.StepsViewModel
import kotlinx.android.synthetic.main.activity_steps.*
import kotlinx.android.synthetic.main.include_viewpager.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.demo.app.base.BaseActivity
import me.hgj.jetpackmvvm.demo.app.ext.bindViewPager2
import me.hgj.jetpackmvvm.demo.app.ext.initToActivity
import com.pucheng.bracelet.app.util.DatetimeUtil

class StepsActivity : BaseActivity<StepsViewModel, ActivityStepsBinding>() {

    private lateinit var stepsTo7DayAdp: BaseQuickAdapter<StepsBean, BaseViewHolder>
    private lateinit var stepsToWeekAdp: BaseQuickAdapter<StepsBean, BaseViewHolder>

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.vm = mViewModel

        if (TextUtils.isEmpty(intent.getStringExtra(KEY_DATE))) {
            mViewModel.queryDate.value = DatetimeUtil.getNowDate(DatetimeUtil.DATE_PATTERN)
        }else{
            mViewModel.queryDate.value = intent.getStringExtra(KEY_DATE)
        }

        tv_title.text = getString(R.string.steps)
        ibtn_back.setOnClickListener { finish() }
        ibtn_right.setOnClickListener {
            startActivityForResult(
                Intent(this, StepsHistoryActivity::class.java),
                REQUEST_CODE_TO_STEP_HISTORY
            )
        }
        //标题集合
        var mDataList: ArrayList<String> = arrayListOf()
        mDataList.add("日")
        mDataList.add("周")
        mDataList.add("月")
        var fragments: ArrayList<Fragment> = arrayListOf()
        fragments.add(StepsToDayFragment.newInstance())
        fragments.add(StepsToWeekFragment.newInstance())
        fragments.add(StepsToMonthFragment.newInstance())
        //初始化viewpager2
        view_pager.initToActivity(this, fragments)
        //初始化 magic_indicator
        magic_indicator.bindViewPager2(view_pager, mDataList)

        rv_to7day.layoutManager = GridLayoutManager(this, 7)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rv_toweek.layoutManager = layoutManager

        val data = arrayListOf<BarChartBean>()
        data.add(BarChartBean("3000", 96.0))
        data.add(BarChartBean("6000", 80.0))
        data.add(BarChartBean("9000", 70.0))
        data.add(BarChartBean("12000", 60.0))
        data.add(BarChartBean("15000", 50.0))
        data.add(BarChartBean("18000", 30.0))
        data.add(BarChartBean("&", 20.0))
        tb_steps.showBarChart(data, resources.getColor(R.color.colorAccent))
        tb_steps.setTitle("比10%的同类用户走得多")
    }

    override fun createObserver() {
        mViewModel.apply {
            queryDate.observe(this@StepsActivity, Observer {
                mViewModel.queryDateToDate()
            })
            stepsTo7Day.observe(this@StepsActivity, Observer {
                getWeekDate()
                stepsTo7DayAdp = object :
                    BaseQuickAdapter<StepsBean, BaseViewHolder>(
                        R.layout.item_7day_data,
                        it as MutableList<StepsBean>?
                    ) {
                    override fun convert(holder: BaseViewHolder, item: StepsBean) {
                        var pg = holder.getView<CircleProgressView>(R.id.pg_data)
                        pg.setMax(stepsTager.value!!)
                        pg.setProgress(item.stepsNum.toInt())
                        holder.setText(
                            R.id.tv_date,
                            DatetimeUtil.formatDate(
                                DatetimeUtil.formatDate(DatetimeUtil.DATE_MD, item.date),
                                DatetimeUtil.DATE_MD
                            )
                        )
                    }

                }
                rv_to7day.adapter = stepsTo7DayAdp

            })
            weekStepsData.observe(this@StepsActivity, Observer {
                LogUtils.e("size : " + it.size)
                stepsToWeekAdp = object :
                    BaseQuickAdapter<StepsBean, BaseViewHolder>(
                        R.layout.item_week_data,
                        it as MutableList<StepsBean>?
                    ) {
                    override fun convert(holder: BaseViewHolder, item: StepsBean) {
                        var pg = holder.getView<ProgressBar>(R.id.pg_week_data)
                        pg.setMax(stepsTager.value!!)
                        pg.setProgress(item.stepsNum.toInt())
                        holder.setText(
                            R.id.tv_week,
                            DatetimeUtil.formatDate(
                                DatetimeUtil.getCustomTime(item.date),
                                DatetimeUtil.DATE_EE
                            )
                        )
                    }

                }
                rv_toweek.adapter = stepsToWeekAdp
            })
        }

    }

    override fun layoutId(): Int = R.layout.activity_steps

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        finish()
    }

}